package dao.mysql;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import util.DataBaseConnection;

import bean.Proveedor;

import dao.Entidad;
public class MySQLProveedorDAO implements Entidad<Proveedor>{

	@Override
	public Proveedor insert(Proveedor t) {
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="INSERT INTO proveedor(razo_soci,ruc,repr,dire,tele)";
			sql = sql + " VALUES(?,?,?,?,?)";
			PreparedStatement ps=con.prepareStatement(sql,PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setString(1, t.getRazo_soci());
			ps.setString(2, t.getRuc());
			ps.setString(3, t.getRepr());
			ps.setString(4, t.getDire());
			ps.setString(5, t.getTele());
			ps.executeUpdate();
			ResultSet rs=ps.getGeneratedKeys();
			if(rs.next()){
				int id=rs.getInt(1);
				t.setId_prov(id);
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return t;
	}

	@Override
	public int update(Proveedor t) {
		Connection con=null;
		int rows = -1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="UPDATE proveedor SET razo_soci=?,ruc=?,repr=?,";
			sql = sql +	"dire=?,tele=? WHERE id_prov=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setString(1, t.getRazo_soci());
			ps.setString(2, t.getRuc());
			ps.setString(3, t.getRepr());
			ps.setString(4, t.getDire());
			ps.setString(5, t.getTele());
			ps.setInt(6, t.getId_prov());
			rows =ps.executeUpdate();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return rows;
	}

	@Override
	public int delete(Proveedor t) {
		Connection con=null;
		int rows=-1;
		try {
			con=DataBaseConnection.openConnection();
			String sql="UPDATE proveedor set esta='N' WHERE id_prov=?";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getId_prov());
			rows=ps.executeUpdate();
			System.out.println(rows);
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return rows;
	}

	@Override
	public Proveedor findById(Proveedor t) {
		// TODO Auto-generated method stub
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="SELECT * FROM proveedor WHERE id_prov=? and esta='R'";
			PreparedStatement ps=con.prepareStatement(sql);
			ps.setInt(1, t.getId_prov());
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				t.setId_prov(rs.getInt("id_prov"));
				t.setRazo_soci(rs.getString("razo_soci"));
				t.setRuc(rs.getString("ruc"));
				t.setRepr(rs.getString("repr"));
				t.setDire(rs.getString("dire"));
				t.setTele(rs.getString("tele"));
			}
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return t;
	}

	@Override
	public List<Proveedor> listAll() {
		// TODO Auto-generated method stub
		List<Proveedor> documentos = new ArrayList<Proveedor>();
		Connection con=null;
		try {
			con=DataBaseConnection.openConnection();
			String sql="SELECT * FROM proveedor where esta='R'";
			Statement st=con.createStatement();
			ResultSet rs=st.executeQuery(sql);
			while(rs.next()){
				Proveedor t = new Proveedor();
				t.setId_prov(rs.getInt("id_prov"));
				t.setRazo_soci(rs.getString("razo_soci"));
				t.setRuc(rs.getString("ruc"));
				t.setRepr(rs.getString("repr"));
				t.setDire(rs.getString("dire"));
				t.setTele(rs.getString("tele"));
				documentos.add(t);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			DataBaseConnection.closeConnection(con);
		}
		return documentos;
	}

	@Override
	public List<Proveedor> listAllCustom(Object... objects) {
		// TODO Auto-generated method stub
		return null;
	}
 

}
